package com.lanling.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanling.eduservice.entity.EduChapter;
import com.lanling.eduservice.entity.EduVideo;
import com.lanling.eduservice.entity.chapter.ChapterVo;
import com.lanling.eduservice.entity.chapter.VideoVo;
import com.lanling.eduservice.mapper.EduChapterMapper;
import com.lanling.eduservice.orther.ZhenhuaException;
import com.lanling.eduservice.service.EduVideoService;
import com.lanling.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 胡振华
 * @since 2020-11-10
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getAllChapter(String courseId) {

        //先根据课程id查询到该课程所有的章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);

        //根据课程id查询出所有的小节
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapper1);

        List<ChapterVo> chapterVos = new ArrayList<>();

        for (int i = 0; i < eduChapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter eduChapter = eduChapters.get(i);
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVos.add(chapterVo);

            List<VideoVo> videoVos = new ArrayList<>();
            for (int i1 = 0; i1 < eduVideos.size(); i1++) {
                EduVideo eduVideo = eduVideos.get(i1);
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setVideoVos(videoVos);
        }
        return chapterVos;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id看看章节中是否有小节，如果能查询出数据，就不进行删除，如果没有查询出数据，就进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0){
            throw new ZhenhuaException(20001,"不能删除");
        }else {
            //可以进行删除
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

}
